package edu.msse.signup.ui;

import java.util.ArrayList;
import java.util.List;

import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.util.CollectionUtils;

import edu.msse.signup.domain.TimeSlot;
import edu.msse.signup.ui.PositionTimeSlotDetail;

import edu.msse.signup.domain.Event;
import edu.msse.signup.domain.Position;
import edu.msse.signup.service.EventService;
import edu.msse.signup.service.PositionService;
import edu.msse.signup.service.TimeSlotService;
import edu.msse.signup.service.UsersService;
import edu.msse.signup.service.VolunteerService;
import edu.msse.signup.service.VolunteerServiceImpl;

public class futureEventDetailHandler {
	private Event event;
	private EventService eventService;
	private VolunteerService volunteerService;
	private TimeSlotService timeslotService;
	private PositionService positionService;

	private List openPositionTimeslot;
	public Event getEvent() {
		return this.event;
	}
	public EventService getEventService() {
		return eventService;
	}
	public void setEventService(EventService eventService) {
		this.eventService = eventService;
	}
	
	public String showEventDetail()
	{
		FacesContext context = FacesContext.getCurrentInstance();
		HttpServletRequest myRequest = (HttpServletRequest)context.getExternalContext().getRequest();
		String EventId=myRequest.getParameter("currentEventId");
		System.out.println("this is my event id"+EventId);
		this.event = eventService.getEventByEventId(Integer.parseInt(EventId));
		return "showEventDetail";
	}
	
	public List getOpenPositionTimeslot() throws Exception
	{
		openPositionTimeslot=new ArrayList();
		getEvent();
		System.out.println("this is id of event"+this.event.getEventId());
		if(volunteerService==null)
		{
			System.out.print("it is null");
		}
			List openPosition=this.volunteerService.getOpenPositionsAndTimeSlots(this.event);
		System.out.println("************** position size*********"+openPosition.size());
		
		
		for(int i=0;i<openPosition.size();i++)
		{
			Position p=(Position)openPosition.get(i);
			if (CollectionUtils.isEmpty(p.getTimeSlots()))
			{
				PositionTimeSlotDetail pos_ts_detail = new PositionTimeSlotDetail();
				pos_ts_detail.setEventId(String.valueOf(this.event.getEventId()));
				pos_ts_detail.setName(p.getName());
				Integer req_vol = new Integer(p.getNumberOfVolunteers()- positionService.getUsers(p).size());
				String req_vol_string = req_vol.toString();
				pos_ts_detail.setRequiredVolunteers(req_vol_string);
				pos_ts_detail.setId(new Long(p.getPositionId()).toString());
				pos_ts_detail.setSlotType("P");
				this.openPositionTimeslot.add(pos_ts_detail);
			}
			else
			{
				for(int j=0;j<p.getTimeSlots().size();j++)
				{
					PositionTimeSlotDetail pos_ts_detail = new PositionTimeSlotDetail();
					TimeSlot time_slot = (TimeSlot)p.getTimeSlots().get(j);
					pos_ts_detail.setEventId(String.valueOf(this.event.getEventId()));
					pos_ts_detail.setName(p.getName());
					pos_ts_detail.setStartTime(time_slot.getStartTime());
					pos_ts_detail.setEndTime(time_slot.getEndTime());
					Integer req_vol = new Integer(time_slot.getNumberOfVolunteers()-timeslotService.getUsers(time_slot).size());
					String req_vol_string = req_vol.toString();
					pos_ts_detail.setRequiredVolunteers(req_vol_string);
					pos_ts_detail.setId(new Long(time_slot.getTimeSlotId()).toString());
					pos_ts_detail.setSlotType("T");
					if (req_vol.intValue() != 0)
						this.openPositionTimeslot.add(pos_ts_detail);
				}
			}
		}
		return this.openPositionTimeslot;
		
	}
	public void setVolunteerService(VolunteerService volunteerServiceImpl) {
		this.volunteerService = volunteerServiceImpl;
	}
	
	public void setPositionService(PositionService positionService) {
		this.positionService = positionService;
	}
	public void setTimeslotService(TimeSlotService timeslotService) {
		this.timeslotService = timeslotService;
	}
	
}
